/**********************************
 ***	Imports
 **********************************/
package fr.montreuil.wargame.controller;

import java.util.ArrayList;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import fr.montreuil.wargame.domain.Batiment;
import fr.montreuil.wargame.domain.Technologie;
import fr.montreuil.wargame.repository.BatimentRepository;
import fr.montreuil.wargame.repository.TechnologieRepository;
import fr.montreuil.wargame.repository.UserRepository;

/**********************************
 *** 	@jgay
 *** 	@9 mai 2012
 **********************************/
@Controller
@RequestMapping(value = "/admin/batiment")
public class AdminBatimentController {
		// --------------------------------------------------------------------------
		// Les repositories
		// --------------------------------------------------------------------------

		@Resource
		UserRepository			userRepository;
		@Resource
		BatimentRepository		batimentRepository;
		@Resource
		TechnologieRepository	technologieRepository;
		
		// --------------------------------------------------------------------------
		// Pour les batiments
		// --------------------------------------------------------------------------

		@RequestMapping(value = "/view")
		public String batimentView(Model model) {
			model.addAttribute("batiments", batimentRepository.findAll());

			return "admin/batiment/view";
		}

		@RequestMapping(method = RequestMethod.GET, value = "/add")
		public String batimentAddGET(Model model,
				@RequestParam(required = false) String message) {
			
			model.addAttribute("message", message);
			model.addAttribute("batiments", batimentRepository.findAll());
			model.addAttribute("technologies", technologieRepository.findAll());

			return "admin/batiment/add";
		}

		@RequestMapping(method = RequestMethod.POST, value = "/add")
		public String batimentAddPOST(@RequestParam String name,
				@RequestParam String level, 
				@RequestParam String costResources1,
				@RequestParam String costResources2,
				@RequestParam String costResources3,
				@RequestParam String costResources4,
				@RequestParam String interestResources1,
				@RequestParam String interestResources2,
				@RequestParam String interestResources3,
				@RequestParam String interestResources4,
				@RequestParam(required = false) String[] batimentReq,
				@RequestParam(required = false) String[] technologieReq) {
			String message = "";
			
			// Si le username est vide
			if (name.isEmpty() || name == "") {
				message = "Name vide !";
				// Si le poassword est vide
			} else if (level.isEmpty() || level == "") {
				message = "Level vide !";
				// Si le lastname est vide
			} else if (costResources1.isEmpty() || costResources1 == "") {
				message = "costResources1 vide !";
			} else if (costResources2.isEmpty() || costResources2 == "") {
				message = "costResources2 vide !";
			} else if (costResources3.isEmpty() || costResources3 == "") {
				message = "costResources3 vide !";
			} else if (costResources4.isEmpty() || costResources4 == "") {
				message = "costResources4 vide !";
			} else if (interestResources1.isEmpty() || interestResources1 == "") {
				message = "interestResources1 vide !";
			} else if (interestResources2.isEmpty() || interestResources2 == "") {
				message = "interestResources2 vide !";
			} else if (interestResources3.isEmpty() || interestResources3 == "") {
				message = "interestResources3 vide !";
			} else if (interestResources4.isEmpty() || interestResources4 == "") {
				message = "interestResources4 vide !";
			}

			// Récuperation d'un user avec l'username
			Batiment exist = batimentRepository.findByName(name);

			if (exist != null && exist.getLvl() == Integer.parseInt(level)) {
				message = "Un batiment éxiste déjà avec ce name et ce level !";
			}

			// Si il n'y a aucune erreur
			if (message.isEmpty()) {
				exist = new Batiment();
				exist.setName(name);
				exist.setLvl(Integer.parseInt(level));

				exist.setCostResources1(Integer.parseInt(costResources1));
				exist.setCostResources2(Integer.parseInt(costResources2));
				exist.setCostResources3(Integer.parseInt(costResources3));
				exist.setCostResources4(Integer.parseInt(costResources4));

				exist.setInterestResources1(Integer.parseInt(interestResources1));
				exist.setInterestResources2(Integer.parseInt(interestResources2));
				exist.setInterestResources3(Integer.parseInt(interestResources3));
				exist.setInterestResources4(Integer.parseInt(interestResources4));

				ArrayList<Batiment> batsReq = new ArrayList<Batiment>();
				if(batimentReq != null){
					for(String batNom : batimentReq){
						batsReq.add(batimentRepository.findByName(batNom));
					}
				}
				exist.setBatRequire(batsReq);
				
				ArrayList<Technologie> techsReq = new ArrayList<Technologie>();
				if(technologieReq != null){
					for(String techNom : technologieReq){
						techsReq.add(technologieRepository.findByName(techNom));
					}
				}

				batimentRepository.save(exist);

				// Redirection
				return "redirect:/admin/batiment/view";
				// Si il y a une erreur
			} else {
				return "redirect:/admin/batiment/add?message=" + message;
			}
		}

		@RequestMapping(method = RequestMethod.GET, value = "/modify")
		public String batimentModifyGET(Model model,
				@RequestParam(required = true) String id,
				@RequestParam(required = false) String message) {
			Batiment tmp = batimentRepository.findById(Long.parseLong(id));
			
			System.out.println(tmp.toString());
			
			model.addAttribute("message", message);
			model.addAttribute("batiment", tmp);
			model.addAttribute("batiments", batimentRepository.findAll());
			model.addAttribute("technologies", technologieRepository.findAll());


			return "admin/batiment/modify";
		}

		@RequestMapping(method = RequestMethod.POST, value = "/modify")
		public String batimentModifyPOST(@RequestParam String id,
				@RequestParam String name,
				@RequestParam String level, 
				@RequestParam String costResources1,
				@RequestParam String costResources2,
				@RequestParam String costResources3,
				@RequestParam String costResources4,
				@RequestParam String interestResources1,
				@RequestParam String interestResources2,
				@RequestParam String interestResources3,
				@RequestParam String interestResources4,
				@RequestParam String[] batimentReq,
				@RequestParam String[] technologieReq) {
			String message = "";

			// Si le username est vide
			if (id.isEmpty() || id == "") {
				message = "Id vide !";
				// Si le poassword est vide
			} else if (name.isEmpty() || name == "") {
				message = "Name vide !";
				// Si le poassword est vide
			} else if (level.isEmpty() || level == "") {
				message = "Level vide !";
				// Si le lastname est vide
			} else if (costResources1.isEmpty() || costResources1 == "") {
				message = "costResources1 vide !";
			} else if (costResources2.isEmpty() || costResources2 == "") {
				message = "costResources2 vide !";
			} else if (costResources3.isEmpty() || costResources3 == "") {
				message = "costResources3 vide !";
			} else if (costResources4.isEmpty() || costResources4 == "") {
				message = "costResources4 vide !";
			} else if (interestResources1.isEmpty() || interestResources1 == "") {
				message = "interestResources1 vide !";
			} else if (interestResources2.isEmpty() || interestResources2 == "") {
				message = "interestResources2 vide !";
			} else if (interestResources3.isEmpty() || interestResources3 == "") {
				message = "interestResources3 vide !";
			} else if (interestResources4.isEmpty() || interestResources4 == "") {
				message = "interestResources4 vide !";
			}

			// Récuperation d'un user avec l'username
			Batiment exist = batimentRepository.findById(Long.parseLong(id));

			if (exist == null) {
				message = "Aucun batiment trouvé avec cet id !";
			}

			// Si il n'y a aucune erreur
			if (message.isEmpty()) {
				exist.setName(name);
				exist.setLvl(Integer.parseInt(level));

				exist.setCostResources1(Integer.parseInt(costResources1));
				exist.setCostResources2(Integer.parseInt(costResources2));
				exist.setCostResources3(Integer.parseInt(costResources3));
				exist.setCostResources4(Integer.parseInt(costResources4));

				exist.setInterestResources1(Integer.parseInt(interestResources1));
				exist.setInterestResources2(Integer.parseInt(interestResources2));
				exist.setInterestResources3(Integer.parseInt(interestResources3));
				exist.setInterestResources4(Integer.parseInt(interestResources4));

				batimentRepository.save(exist);

				// Redirection
				return "redirect:/admin/batiment/view";
				// Si il y a une erreur
			} else {
				return "redirect:/admin/batiment/modify?message=" + message;
			}
		}

}
